package com.xbongbong.paas.handle;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.DistributorMapVO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.DistributorAreaEntity;
import com.xbongbong.saas.domain.entity.ProductCategoryEntity;
import com.xbongbong.saas.enums.business.DistributorEnum;
import com.xbongbong.saas.model.DistributorAreaModel;
import com.xbongbong.saas.model.ProductCategoryModel;
import com.xbongbong.saas.service.toolbox.help.SaasListDataPermissionHelp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/24 16:33
 * @since v1.0
 */
@Component
public class DistributorAnalysisDataHandle extends AbstractWebDataAnalysisHandle {

    private final static Logger LOGGER = LoggerFactory.getLogger(DistributorAnalysisDataHandle.class);

    @Resource
    private SaasListDataPermissionHelp saasListDataPermissionHelp;
    @Resource
    private DistributorAreaModel distributorAreaModel;
    @Resource
    private ProductCategoryModel productCategoryModel;

    @Override
    public PaasFormDataEsListVO analysisData(FormDataListDTO formDataListDTO) throws XbbException {
        PaasFormDataEsListVO formDataListVO;
        try {
            // 权限处理
            List<ConditionsEntityExt> conditionList = formDataListDTO.getConditions();
            saasListDataPermissionHelp.listDataPermission(conditionList,formDataListDTO.getLoginUser(), formDataListDTO.getBusinessType(), formDataListDTO.getSubBusinessType());
            formDataListDTO.setConditions(conditionList);
            // 所在地区、可采购商品下级回显处理
            handleSubShow(formDataListDTO);
            // 区域地图处理
            if (Objects.equals(formDataListDTO.getIsDistributorMap(), 1)) {
                handleAddressCondition(formDataListDTO);
            }
            formDataListVO = getEsList(formDataListDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("DistributorAnalysisDataHandle.analysisData 出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return formDataListVO;
    }

    private void handleAddressCondition(FormDataListDTO formDataListDTO) {
        DistributorMapVO distributorMapVO = formDataListDTO.getDistributorMapVO();
        List<Object> itemList = new ArrayList<>();
        if (Objects.nonNull(distributorMapVO) && Objects.nonNull(distributorMapVO.getName())) {
            List<String> distributorAddress = formDataListDTO.getDistributorAddress() == null ? new ArrayList<>() : formDataListDTO.getDistributorAddress();
            if (Objects.isNull(distributorMapVO.getProvince())) {
                distributorMapVO.setProvince(distributorMapVO.getName());
                itemList.add(distributorMapVO.getName());
                distributorAddress.add(distributorMapVO.getName());
            } else {
                itemList.add(distributorMapVO.getProvince());
                distributorAddress.add(distributorMapVO.getProvince());
                if (Objects.isNull(distributorMapVO.getCity())) {
                    distributorMapVO.setCity(distributorMapVO.getName());
                    itemList.add(distributorMapVO.getName());
                    distributorAddress.add(distributorMapVO.getName());
                } else {
                    itemList.add(distributorMapVO.getCity());
                    distributorAddress.add(distributorMapVO.getCity());
                    distributorMapVO.setDistrict(distributorMapVO.getName());
                    itemList.add(distributorMapVO.getName());
                    distributorAddress.add(distributorMapVO.getName());
                }
            }
            ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt(DistributorEnum.ADDRESS.getAttr(),"",FieldTypeEnum.ADDRESS.getType(), ConditionEnum.EQUAL.getSymbol(),itemList);
            formDataListDTO.getConditions().add(conditionsEntityExt);
            formDataListDTO.setDistributorAddress(distributorAddress);
        } else if (CollectionsUtil.isNotEmpty(formDataListDTO.getDistributorAddress())) {
            distributorMapVO = Objects.isNull(distributorMapVO) ? new DistributorMapVO() : distributorMapVO;
            distributorMapVO.setProvince(formDataListDTO.getDistributorAddress().get(0));
            distributorMapVO.setName(formDataListDTO.getDistributorAddress().get(formDataListDTO.getDistributorAddress().size() -1));
            if (formDataListDTO.getDistributorAddress().size() > 1) {
                distributorMapVO.setCity(formDataListDTO.getDistributorAddress().get(1));
            }
            if (formDataListDTO.getDistributorAddress().size() > 2) {
                distributorMapVO.setDistrict(formDataListDTO.getDistributorAddress().get(2));
            }
            formDataListDTO.setDistributorMapVO(distributorMapVO);
        }
    }

    /**
     * 所在地区、可采购商品下级回显处理
     * @param formDataListDTO
     * @throws XbbException
     * @author xinpeng.jiang
     * @date 2021/1/6 10:57
     * @version 1.0
     */
    private void handleSubShow(FormDataListDTO formDataListDTO) {
        List<ConditionsEntityExt> conditionList = formDataListDTO.getConditions();
        String corpid = formDataListDTO.getCorpid();
        for (ConditionsEntityExt condition : conditionList) {
            if (Objects.isNull(condition.getFieldType()) || CollectionsUtil.isEmpty(condition.getValue())) {
                continue;
            }
            Integer fieldType = condition.getFieldType();
            Object value = condition.getValue().get(0);
            if (Objects.equals(FieldTypeEnum.getFieldTypeEnum(fieldType), FieldTypeEnum.AREA)) {
                HashMap<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                String router = StringConstant.VERTICAL_LINE + value + StringConstant.VERTICAL_LINE;
                params.put("routerLike", router);
                params.put(StringConstant.CORPID, corpid);
                params.put(StringConstant.DEL, BasicConstant.ZERO);
                List<DistributorAreaEntity> areaEntityList = distributorAreaModel.findEntities(params);
                List<Object> areaIdList = new ArrayList<>();
                areaEntityList.forEach(i -> areaIdList.add(i.getId()));
                condition.setValue(areaIdList);
            }
            if (Objects.equals(FieldTypeEnum.getFieldTypeEnum(fieldType), FieldTypeEnum.PURCHASEABLE_GOODS)) {
                HashMap<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                String router = StringConstant.VERTICAL_LINE + value + StringConstant.VERTICAL_LINE;
                params.put("routerLike", router);
                params.put(StringConstant.CORPID, corpid);
                params.put(StringConstant.DEL, BasicConstant.ZERO);
                List<ProductCategoryEntity> productCategoryEntityList = productCategoryModel.findEntitys(params);
                List<Object> productCategoryIdList = new ArrayList<>();
                productCategoryEntityList.forEach(i -> productCategoryIdList.add(i.getId()));
                condition.setValue(productCategoryIdList);
            }
        }
    }


}
